﻿//给fla的所有库元件应用另外一个fla文件的库元件配置
/**
 *	搜索另外一个FLA文件的库元件, 同名的库元件使用与当前FLA文件中相同配置, 包括链接类名等等.
 *	tamt 2009.09.09	ver0.1 当前只针对链接类名属性.
 */
//-------------界面文件的生成----------------------
var tmpXmlFile = fl.configURI + "/tmp.xml";
var xmlGui = buildXulGui();
FLfile.write(tmpXmlFile, xmlGui);
function buildXulGui()
{
	var t = fl.getDocumentDOM().pathURI;
	var folderPath = t.substr(0, t.lastIndexOf('/'));
  return '<dialog id="dialog" title="复制库元件的属性配置" buttons="accept, cancel"> \
      \
      <script> \
      function browseFile() \
      { \
        var fileURL = fl.browseForFileURL("open", "选择目标FLA文件"); \
        if(fileURL != null) \
        { \
          fl.xmlui.set("file", fileURL); \
        } \
      } \
      </script> \
      \
     <vbox> \
      <label width="300" value="搜索另外一个FLA文件的库元件, 同名的库元件使用与当前LA文件中相同配置, 包括链接类名等等."/> \
      <separator/> \
      <label value="目标FLA文件:" /> \
      <textbox value="' + folderPath +'" width="300" id="file"/> \
      <button id="openFolder" label="选择..." oncommand="browseFile()" disabled="true"/>	\
    </vbox> \
    \
  </dialog>';
}
//-------------------------------------------------
var setting = fl.getDocumentDOM().xmlPanel(tmpXmlFile);
if(setting.dismiss == 'accept'){
	var srcDoc = fl.getDocumentDOM();
	var tgDoc = fl.openDocument(setting.file);
	var items = srcDoc.library.items;
	var srcItem;
	var tgItem;
	for(var i=0; i<items.length; i++){
		srcItem = items[i];
		if(srcItem.itemType == 'movie clip' ||  srcItem.itemType == 'bitmap'){
			//搜索是否有名称路径全匹配的元件.
			if(tgDoc.library.itemExists(srcItem.name)){				
				tgDoc.library.selectItem(srcItem.name);
				tgItem = tgDoc.library.getSelectedItems()[0];
			}else{
				//如果没有找到名称路径全匹配的, 尝试寻找是否有名称匹配的.
				var srcItemName = getItemName(srcItem.name);
				var tgItems = tgDoc.library.items;
				var tItem;
				for(var j=0; j<tgItems.length; j++){
					tItem = tgItems[j];
					if(getItemName(tItem.name) == srcItemName){
						tgItem = tItem;
						break;
					}
				}
				if(!tItem){
					fl.trace('没有在' + tgDoc.name + '中找到元件: ' + srcItemName);
					continue;
				}
			}
			
			tgItem.linkageExportForAS = srcItem.linkageExportForAS;
			if(srcItem.linkageExportForAS){
				tgItem.linkageExportInFirstFrame = srcItem.linkageExportInFirstFrame;
				tgItem.linkageBaseClass = srcItem.linkageBaseClass;
				tgItem.linkageClassName = srcItem.linkageClassName;
			}
			tgDoc.library.updateItem(srcItem.name);
		}
	}
}

function getItemName(namePath){
	if(namePath.indexOf('/')){
		return namePath.substring(namePath.lastIndexOf('/') + 1, namePath.length);
	}
	return namePath;
}